package com.netsdk.socket;


import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Test implements Runnable {
	
    private Socket socket;
	private Charset charset = Charset.forName("GBK");

	public Test(Socket socket) {
		this.socket=socket;
	}
	
	public void run() {
		while (true) {
			String backup = "";
		  try {
		  		if(testSocket()){
					String data = DataStore.get();
					backup = data;
					System.out.println("待发送数据 ：" + new Date() + " " + data);
					OutputStream os = socket.getOutputStream();
					os.write((data+"|").getBytes(StandardCharsets.UTF_8));
					os.flush();
				}else {
					System.out.println("连接关闭");
					break;
				}
		  }catch(Exception e) {
			  e.printStackTrace();
			  if(!"".equals(backup)){
				  DataStore.set(backup);
			  }
			  try {
				  socket.close();
			  } catch (IOException ioException) {
				  ioException.printStackTrace();
			  }
			  break;
		  }finally {
		  }
		}
	}

	private boolean testSocket(){
		return socket.isConnected() && !socket.isClosed() && !socket.isInputShutdown() && !socket.isOutputShutdown();
	}
	
	
//	public void run() {
//		while (true) { 
//			
//		  try {
//
//			  
////				 String sql = "	select distinct t1.mmsi from trajectory t1,( \r\n" +
////					"		select time,nlane,direction,lon,lat,sum(1) from trajectory  group by time,nlane,direction,lon,lat \r\n" +
////					"		having(sum(1)>1)) t2 \r\n" +
////				"	where t1.time=t2.time and t1.nlane=t2.nlane and t1.direction=t2.direction and t1.lon=t2.lon  and t1.lat=t2.lat \r\n" +
////				"	and t1.mmsi not in ( \r\n" +
////				"				select distinct tt.mmsi from ( \r\n" +
////				"				select min(t1.mmsi) as mmsi,t1.time,t1.nlane,t1.direction,t1.lon,t1.lat \r\n" +
////				"				from trajectory t1,( \r\n" +
////				"				select time,nlane,direction,lon,lat,sum(1) from trajectory  group by time,nlane,direction,lon,lat having(sum(1)>1) \r\n" +
////				"				) t2 \r\n" +
////				"				where t1.time=t2.time and t1.nlane=t2.nlane and t1.direction=t2.direction and t1.lon=t2.lon and t1.lat=t2.lat \r\n" +
////				"				group by t1.time,t1.nlane,t1.direction,t1.lon,t1.lat \r\n" +
////				"				) tt ) " ;
//				 
////			  List<Map<String, Object>> list = JdbcUtils.queryBean(sql, null);
////			  String  mmsi = "";
////			   if (null != list && list.size() > 0) {
////				   String sql55 = " delete FROM trajectory  where  mmsi in (" ;
////			 	 for (int i = 0; i < list.size(); i++) {
////					     if(i<list.size()-1) {
////					    	 mmsi = mmsi + "'" + list.get(i).get("mmsi") + "',";
////					     }else {
////					    	 mmsi = mmsi + "'" + list.get(i).get("mmsi") + "')";
////					     }
////				 }
////			 	System.out.println("�h�����Nӛ� ��" + sql55+mmsi);
////					int rr = JdbcUtils.delete(sql55+mmsi);
////					System.out.println("�h�����Nӛ� ��" + rr);
////				
////		  	 }
//			  
////			 String sql3 = "select t1.name,t1.time,t1.lon,lat,t1.sog,t1.cog,t1.heading,t1.mmsi,t1.imo,t1.callsign,t1.shiptypename, \r\n" +
////					  "t1.width,t1.length,t1.timeout,t1.shiptypeclassname,t1.inlandshipmark \r\n" +
////					  "from trajectory t1,(select distinct temp.mmsi from (select t.time,t.mmsi from trajectory t where abs(TIMESTAMPDIFF(SECOND,t.time,CURRENT_TIMESTAMP)) < 190  ) temp) m \r\n" + 
////					  " ,(select t.mmsi ,sum(1) from trajectory t group by t.mmsi having(sum(1)=7) ) v \r\n" + 
////					  "where t1.mmsi=m.mmsi and t1.mmsi=v.mmsi and t1.STATUS = 1 order by t1.time ";
////			 
//			 
//			  
////			  String sql3 = " select t1.name,t1.time,t1.lon,lat,t1.sog,t1.cog,t1.heading,t1.mmsi,t1.imo,t1.callsign,t1.shiptypename,\r\n" + 
////			 		"					  t1.width,t1.length,t1.timeout,t1.shiptypeclassname,t1.inlandshipmark 	  \r\n" + 
////			 		"from trajectory t1\r\n" + 
////			 		"	,(select t.mmsi ,sum(1) \r\n" + 
////			 		"	from trajectory t \r\n" + 
////			 		"	group by t.mmsi having(sum(1)=7) \r\n" + 
////			 		"	) v \r\n" + 
////			 		"	where  t1.mmsi=v.mmsi \r\n" + 
////			 		"	and t1.STATUS = 1 \r\n" + 
////			 		"	order by t1.time \r\n" + 
////			 		"";
////
////
////			   List<Map<String, Object>> list2 = JdbcUtils.queryBean(sql3, null);
////
////			   JSONObject json = new JSONObject();
////			   StringBuffer sb = new StringBuffer();
////			  
////			   String  newmmsi = "";
////			   if (null != list2 && list2.size() > 0) {
////				   String sql4 = " delete FROM trajectory  where  mmsi in (" ;
////			 	 for (int i = 0; i < list2.size(); i++) {
////					 // ����ServerSocketʵ����ָ���˿ڼ����ͻ��˵���������
////					 sb.append(JSON.toJSONString(list2.get(i)));
////					 
////				     if(i<list2.size()-1) {
////				    	 newmmsi = newmmsi + "'" + list2.get(i).get("mmsi") + "',";
////				     }else {
////				    	 newmmsi = newmmsi + "'" + list2.get(i).get("mmsi") + "')";
////				     }
////				     
////				 }
////			 	 int r = JdbcUtils.delete(sql4+newmmsi);
//			 	 
//			  	String data = DataStore.get();
//				 System.out.println("���������� ��" + new Date() + "\n" + data);
////				 System.out.println("���������� ��" + list2.size());
////				 System.out.println("�h������ ��" + r);
//				
//				 OutputStream os = socket.getOutputStream();
//				 os.write(data.getBytes());
//				 os.flush();
//				 
//				// JdbcUtils.close();
//		  	 }
//
////			Thread.sleep(2000);
//		  }catch(Exception e) {
//			  e.printStackTrace();
//		  }finally {
//		  }
//		}
//	}

	

}
